package com.rxyb.security.gate.controller;

import cn.hutool.json.JSONUtil;
import com.rxyb.security.db.bean.SysUsercustom;
import com.rxyb.security.utils.RoleUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;

/**
 * @author YJH
 * @version 1.0
 * @description
 * @date 2020/7/7 11:10
 */
@Controller
@Slf4j
public class InitController {

    private final RoleUtils roleUtils;

    public InitController(RoleUtils roleUtils) {
        this.roleUtils = roleUtils;
    }


    @RequestMapping(value = "/login")
    public ModelAndView init(ModelAndView model, String msg) {

        model.addObject("error", msg);
        model.setViewName("login");
        return model;
    }

    @RequestMapping(value = "/logout")
    public String logout() {
        SecurityContextHolder.clearContext();
        ;
        return "login";
    }

    @RequestMapping(value = "/login-error")
    public String loginError() {
        return "login-error";
    }


    @RequestMapping(value = "/tableList")
    public ModelAndView tableList(ModelAndView model, HttpServletRequest request) {
        String servletPath = request.getServletPath();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SysUsercustom usercustom = JSONUtil.toBean(JSONUtil.toJsonStr(authentication.getPrincipal()), SysUsercustom.class);
        log.info("获取当前得信息是：[{}]", JSONUtil.toJsonStr(roleUtils.getMenuPre(usercustom, servletPath)));
        model.addObject("pre", roleUtils.getMenuPre(usercustom, servletPath));
        model.setViewName("table-list");
        return model;
    }


    @RequestMapping(value = "/home")
    public ModelAndView home(String nameUrl) {
        ModelAndView m = new ModelAndView();
        // 获取当前的角色权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SysUsercustom usercustom = JSONUtil.toBean(JSONUtil.toJsonStr(authentication.getPrincipal()), SysUsercustom.class);
        m.addObject("menu", roleUtils.menu(
                usercustom
                        .getSysRoleList()
                        .stream()
                        .filter(e -> e.getMenuAlias().indexOf("ROLE_PRE") < 0)
                        .collect(Collectors.toList()), nameUrl));
        m.addObject("url", nameUrl);
        m.addObject("username", usercustom.getUsername());
        m.setViewName("home");
        return m;
    }


    @RequestMapping(value = "/limit")
    public String limit() {
        return "limit";
    }

    @Secured("ROLE_C")
    @RequestMapping(value = "kq")
    @ResponseBody
    public String kq() {
        return "您有权限访问哦";
    }

    @GetMapping(value = "user2")
    @ResponseBody
    public Object user2(Authentication authentication) {
        return authentication;
    }

}
